﻿using HR.Config;
using HR.IService;
using HR.Models;
using Microsoft.AspNetCore.Mvc;

namespace HR.Service
{
    public class JobRegistService : IJobRegistService
    {
        private readonly HRDBContext db;

        public JobRegistService(HRDBContext db)
        {
            this.db = db;
        }

        public Task<int> EngageMajorReleaseAdd(engage_major_release emr)
        {
            return Task.Run(() =>
            {
                db.engage_major_releases.Add(emr);
                return db.SaveChanges();
            });
        }

        public Task<Paging<engage_major_release>> EngageMajorReleaseFill(int pgsize, int pgcurrent)
        {
            return Task.Run(() =>
            {
                List<engage_major_release> list = db.engage_major_releases.ToList();
                List<engage_major_release> tabs = list.Skip((pgcurrent - 1) * pgsize).Take(pgsize).ToList();
                Paging<engage_major_release> pg = new Paging<engage_major_release>()
                {
                    rows = list.Count,
                    tabs = tabs
                };
                return pg;
            });
        }

        public Task<int> EngageMajorReleaseUpdate(engage_major_release emr)
        {
            return Task.Run(() =>
            {
                engage_major_release? emrs = db.engage_major_releases.Find(emr.mre_id);
                emrs.first_kind_id = emr.first_kind_id;
                emrs.first_kind_name = emr.first_kind_name;
                emrs.second_kind_id = emr.second_kind_id;
                emrs.second_kind_name = emr.second_kind_name;
                emrs.third_kind_id = emr.third_kind_id;
                emrs.third_kind_name = emr.third_kind_name;
                emrs.major_kind_id = emr.major_kind_id;
                emrs.major_kind_name = emr.major_kind_name;
                emrs.major_id = emr.major_id;
                emrs.major_name = emr.major_name;
                emrs.human_amount = emr.human_amount;
                emrs.engage_type = emr.engage_type;
                emrs.deadline = emr.deadline;
                emrs.changer = emr.changer;
                emrs.change_time = emr.change_time;
                emrs.major_describe = emr.major_describe;
                emrs.engage_required = emr.engage_required;
                return db.SaveChanges();
            });
        }
        public Task<int> EngageMajorReleaseDelete(short mre_id)
        {
            return Task.Run(() =>
            {
                engage_major_release? emr = db.engage_major_releases.Find(mre_id);
                db.engage_major_releases.Remove(emr);
                return db.SaveChanges();
            });
        }
    }
}
